package cc.catface.order;

import cc.catface.order.feign.TestFeign;
import cc.catface.order.ribbon.TestRibbon;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {

    @GetMapping("order")
    public String order() {
        return "order--" + new Date().toLocaleString();
    }


    /*测试ribbon访问服务*/
    @Autowired
    private TestRibbon testRibbon;

    @GetMapping("testRibbon")
    public String testRibbon() {
        return testRibbon.getAccount();
    }

    /*测试feign访问服务*/
    @Autowired
    private TestFeign testFeign;

    @GetMapping("testFeign")
    public String testFeign() {
        return testFeign.getAccount();
    }

    /*测试hystrix超时*/
    @GetMapping("timeout")
    @HystrixCommand(fallbackMethod = "hystrix_timeout", commandProperties =
            //3秒超时
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    public String timeout() throws InterruptedException {
        Thread.sleep(4000);
        return "没有超时";
    }

    public String hystrix_timeout() {
        return "3秒超时了";
    }
}
